//
//  HotRightCollectionViewCell.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/7.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "HotRightCollectionViewCell.h"
#import "HotDetailsTableViewCell.h"
#import "NetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "HotRightModel.h"
#import "TAMacro.h"

@interface HotRightCollectionViewCell ()
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)HotDetailsTableViewCell *cell;
@end
@implementation HotRightCollectionViewCell
- (NSMutableArray *)array{
    if (!_array) {
        self.array = [NSMutableArray array];
    }    return _array;
}
- (NSArray *)contentArray {
    if (!_contentArray) {
        self.contentArray = [NSMutableArray array];
    }
    return _contentArray;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubview:frame];
    }
    return self;
}
- (void)setStr:(NSString *)Str{
    // NSLog(@"%@",Str);
    _Str = Str;
    [NetworkHandler getDataByURLString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/items/%@/comments?limit=20&offset=0",_Str] BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"comments"];
        for (NSDictionary *dic in arr) {
            //   self.contentArray = [dic objectForKey:@"content"];
            HotRightModel *model = [[HotRightModel alloc]init];
            model.content = [dic objectForKey:@"content"];
            NSDictionary *dic2 = dic[@"user"];
            model.avatar_url = [dic2 objectForKey:@"avatar_url"];
            model.nickname = [dic2 objectForKey:@"nickname"];
            [self.array addObject:model];
        }
        [self.tableView reloadData];
    }];
}
- (void)creatSubview:(CGRect)frame {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - SCREEN_HEIGHT /10)style:0];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.contentView addSubview:self.tableView];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    self.cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (self.cell  == nil) {
        self.cell = [[HotDetailsTableViewCell alloc]initWithStyle:0 reuseIdentifier:ID];
    }
    HotRightModel *model = self.array[indexPath.item];
    self.cell.hotRightModel = model;
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
@end
